package com.sita.bluetactivity.MineFragment;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bluetactivity.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import de.greenrobot.event.EventBus;

/**
 * Created by Jason on 2016/10/10.
 */

public class BtEquipmentAdapter extends RecyclerView.Adapter<BtEquipmentAdapter.MyViewHolder>{
    private Context context;
    private List<BluetoothDevice> bluetoothDevices;
    private onClickItem onClickItem;

    public BtEquipmentAdapter(Context context) {
        this.context = context;
        bluetoothDevices = new ArrayList<>();
    }

    public void setData(BluetoothDevice data,onClickItem clickItem){
        onClickItem = clickItem;
        if (data.getName()!=null&&data.getAddress()!=null){
            bluetoothDevices.add(data);
            notifyDataSetChanged();
        }

    }
    public void clearAll(){
        bluetoothDevices.clear();
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_btdevice,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
          holder.btName.setText(bluetoothDevices.get(position).getName());
         holder.position = position;
    }

    @Override
    public int getItemCount() {
        return bluetoothDevices.size()>0?bluetoothDevices.size():0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView btName;
        private int position;
        private RelativeLayout btLayout;
        public MyViewHolder(View itemView) {
            super(itemView);
            btName = (TextView) itemView.findViewById(R.id.bt_name);
            btLayout = (RelativeLayout) itemView.findViewById(R.id.bt_layout);
            btLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                 onClickItem.onBtClick(bluetoothDevices.get(position));
                }
            });
        }
    }
}
